package com.example.springboot3005web.controller;

import com.example.springboot3005web.vo.Role;
import com.example.springboot3005web.vo.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Controller
public class ViewController {

    // model就是将数据放在model里，然后将model给到view
    @RequestMapping("/model")
    public String modelMethod(Model model) {
        model.addAttribute("title", "java开发");
        model.addAttribute("time", LocalDateTime.now());
        return "quick";
    }

    // ModelAndView方式就是将数据封装在model中，将数据给到对应的view，适合Thymeleaf作为前端
    @RequestMapping("/hello")
    public ModelAndView hello() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("name","张三");
        mv.addObject("age", 20);
        mv.setViewName("hello");
        return mv;
    }

    @GetMapping("/user")
    @ResponseBody
    public User user() {
        User user = new User();
        user.setAge(20);
        user.setUsername("mgs");
        return user;
    }

    @GetMapping("getUserRole")
    @ResponseBody
    public User getUserRole() {
        Role role = new Role(1, "管理员", "负责系统维护");
        User user = new User("zhangsan",20,role);
        return user;
    }


    // 可以携带响应的状态给前端
    @GetMapping("/getUserInfo")
    public ResponseEntity<User> getUserInfo(){
        Role role = new Role(1, "管理员", "负责系统维护");
        User user = new User("zhangsan",20,role);
        ResponseEntity<User> response = new ResponseEntity<>(user, HttpStatus.NOT_FOUND);
        return response;
    }

    @GetMapping("/map/json")
    @ResponseBody public Map<String,Object> getMap(){
        Map<String,Object> map = new HashMap<>();
        map.put("id",1001);
        map.put("address","大兴区");
        map.put("city","北京");
        return map;
    }
}
